<?php
  require_once('./db.php');
  require_once('./header.php');

  $msg = '';
if(isset($_POST['uname']) &&
  !empty($_POST['uname']))
{
  if($_POST['captcha'] == $_SESSION['secretword'])
  {
    // we don't need to save captcha in db
    unset($_POST['captcha']);
  ##############################################3

  $validExtn = array('png',
                     'jpeg',
                     'jpg',
                     'gif' );
  $mb = 1;
  $allowedFileSize = $mb * 1024 * 1024;
  $uploadDir = './uploads';

  if(isset($_FILES['avatar']['error']) && $_FILES['avatar']['error']==0) {
    if($_FILES['avatar']['size'] < $allowedFileSize)
    {

      $fileName = $_FILES['avatar']['name'];
      //pr($fileName);
      $fileDetails = pathinfo($fileName);
      $fileExtn = $fileDetails['extension'];
      //pr($fileExtn);
      if(!in_array($fileExtn,$validExtn)) {
        $msg = "Please uplaod file with valid extn(".implode(',',$validExtn).")";
      } else {
        $src = $_FILES['avatar']['tmp_name'];
        $fileName = date('ymdhis').'_'.uniqid().'_'.$_FILES['avatar']['name'];

        if(!is_dir($uploadDir)) {
          mkdir($uploadDir,0755);
        }
        $dest = "$uploadDir/$fileName";

        $isMove = move_uploaded_file($src, $dest);
        //var_dump($isMove);
        if($isMove == true) {
          $msg = "File uploaded successfully.";
          $_POST['avatar'] = $fileName;

          ######## CRAETE THUMB ######
          require_once('thumbnail/image.class.php');
          $thumb_dir = './uploads/thumb';
          if(!is_dir($thumb_dir)) {
            mkdir($thumb_dir,0755);
          }
          $img = new Zubrag_image;
          $img->max_x        = 100;
          $img->max_y        = 100;
          $img->GenerateThumbFile($dest, "$thumb_dir/$fileName");
          // here $dest is treated as Source
          ############################

        } else {
          $msg = "There might be some error during upload.Please try again.";
        }
      }
    } else {
      $msg = "Please upload file with $mb MB Max";
    }
  } else if(isset($_FILES['avatar']['error'])){
      $msg = "Please upload file with $mb MB Max";
  }
  ##############################################
  $_POST = array_map('mysql_real_escape_string',$_POST);
  $_POST = array_map('strip_tags',$_POST);
  $_POST = array_map('trim',$_POST);

  $_POST['added_date'] = date('Y-m-d H:i:s');
  $_POST['status'] = '1';
  $_POST['password'] = md5($_POST['password']);
  $insertQuery  = "INSERT INTO `users` SET ";
  foreach($_POST as $colName=>$value) {
    $insertQuery .= "`$colName`='$value' ,";
  }
  $insertQuery = rtrim($insertQuery,' ,');
  mysql_query($insertQuery);
  $id = mysql_insert_id();
  echo "Record Inserted On ID :$id";
  pq($insertQuery);
  } else {
    echo "Security code incorrect";
  }
}

if($msg != '') {
  echo "<div class=\"error\">$msg</div>";
}
?>

<form action="" method="post" enctype="multipart/form-data">
  <table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
    <thead>
      <tr>
        <th colspan="2">-:Please fill the details below:-</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="right">Username :</td>
        <td ><input type="text" name="uname" value="<?=@$_POST['uname']?>" placeholder="User Name" required /></td>
      </tr>
      <tr>
        <td align="right">Email :</td>
        <td ><input type="email" name="email" value="<?=@$_POST['email']?>" placeholder="Please add e-mail" required /></td>
      </tr>
      <tr>
        <td align="right">Password :</td>
        <td ><input type="password" name="password" autocomplete="off" placeholder="Please add password" required /></td>
      </tr>
      <tr>
        <td align="right">Avatar :</td>
        <td ><input type="file" name="avatar"  required /></td>
      </tr>
      <tr>
        <td align="right">Security Code :</td>
        <td >
<!-- Captcha Start -->
<img src="captcha/captcha.php" id="captcha" /><br/>
<a href="#" onclick="    document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>
<input type="text" placeholder="Security Code" name="captcha" id="captcha-form" required /><br/>
<!-- Captcha End-->

        </td>
      </tr>
      <tr>
        <td align="right">&nbsp;</td>
        <td ><input type="submit" value="Please add !!" /></td>
      </tr>
    </tbody>
  </table>

</form>
<style type="text/css">
  .error{
    border:1px solid red;
    padding:10px;
    margin:10px;
    background-color:pink;
    border-radius:5px;
  }
</style>
